﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI;
using System.Reflection;
using System.ComponentModel;

using Developmentalmadness.Web.UI.AjaxAbstractions;

namespace Developmentalmadness.Web.UI.ExtJsControlLibrary
{
    public class ExtJsDateMenu : ExtJsMenu, ISubMenu
    {
        private List<ScriptReference> _references = new List<ScriptReference>();

        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.menu.DateMenu";

            // date picker
            if (picker != null)
            {
                ScriptControlDescriptorBase descriptor = (ScriptControlDescriptorBase)picker.GetScriptDescriptors()[0];

                _references.AddRange(picker.GetScriptReferences());

                desc.AddPropertyBase("picker", descriptor.WriteScript(ScriptType.ConfigOnly));
            }
            return list;
        }


        #region Public Properties

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ExtJsDatePicker picker { get; set; }

        #endregion

        #region IMenuItem Members

        public string GetMenuItemScript()
        {
            ScriptControlDescriptorBase descriptor = (ScriptControlDescriptorBase) GetScriptDescriptors()[0];
            ExtJsScriptControlDescriptor config = new ExtJsScriptControlDescriptor("Ext.menu.DateMenu", ID);
            config.AddPropertyBase("text", String.Format("'{0}'", text));
            config.AddPropertyBase("menu", descriptor.WriteScript(ScriptType.NewOnly));
            return config.WriteScript(ScriptType.ConfigOnly);
        }

        #endregion

        #region ISubMenu Members

        public String text { get; set; }
        
        #endregion

    }
}
